﻿using System;
using System.Collections.Generic;
using System.IO;
using PPI.Client.Logic.Delegate;
using PPI.Client.Utils;
using PPI.Entities.Distribution;

namespace PPI.Client.Presenter
{
    public interface ISplashDistribution
    {
        string ActionMessage { set; }
        Int32 ActionProgress { set; }
        Int32 InspectionId { get; }
        EDistributionPole Pole { set; get;}
        void Finish();
    }

    public class SplashDistributionPresenter
    {
        private ISplashDistribution m_View;
        private DownloadFile m_Downloader;
        private UpdateBar m_DelegateProgressBar;
        private EDistributionPole m_Inspection;
        private Stack<EDistributionPoleImage> m_ImagesUrlToDownload;

        public SplashDistributionPresenter(ISplashDistribution view)
        {
            m_View = view;
            Initialize();
        }

        private void Initialize()
        {
            try
            {
                m_DelegateProgressBar = UpdateProgressBar;
                m_Downloader = new DownloadFile(m_DelegateProgressBar);
                m_Downloader.FinishDownloadFile += m_Downloader_FinishDownloadFile;
                DownloadInspection();
                m_Inspection = m_View.Pole;

                if (m_Inspection != null)
                {
                    PrepareImagesToDownload();
                    DownloadInspectionImages();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        void m_Downloader_FinishDownloadFile(object sender, EventArgs e)
        {
            try
            {
                DownloadInspectionImages();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void UpdateProgressBar(int percentage)
        {
            m_View.ActionProgress = percentage;
        }

        private void DownloadInspection()
        {
            m_View.ActionMessage = "Downloading Data ...";
        }

        private void DownloadInspectionImages()
        {
            m_View.ActionMessage = "Downloading Images ...";

            if(m_ImagesUrlToDownload.Count > 0)
            {
                EDistributionPoleImage tmpImage = m_ImagesUrlToDownload.Pop();
                m_Downloader.SetUrl(tmpImage.GetImageUrl(m_Inspection.Id));
                m_Downloader.SetFileDestination(tmpImage.GetTemporaryFolder());

                if (!File.Exists(tmpImage.GetTemporaryFolder()))
                    m_Downloader.StartDownload();
                else
                    DownloadInspectionImages();
            }
            else
            {
                m_View.ActionMessage = "Finish Downloading images ...";

                foreach (EDistributionPoleImage i in m_Inspection.Images)
                {
                    if (File.Exists(i.GetTemporaryFolder())) 
                    {
                        i.FullName = i.GetTemporaryFolder();
                    }
                }

                m_View.Pole = m_Inspection;
                m_View.Finish();
            }
        }

        private void PrepareImagesToDownload()
        {
            m_View.ActionMessage = "Preparing Images ...";
            m_ImagesUrlToDownload = new Stack<EDistributionPoleImage>(m_Inspection.Images);

            foreach (EMeter meter in m_Inspection.Meters)
                if (!String.IsNullOrEmpty(meter.Image.FileName))
                    m_ImagesUrlToDownload.Push(meter.Image);
        }
    }
}
